home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: QUESTION about pointers and structures
- Date: 12 Jan 1996 18:19:43 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d68nv$g4r@news.iag.net>
- References: <4d1rqg$bse@mirv.unsw.edu.au> <4d31u1$kdv@tahko.lpr.carel.fi>
- NNTP-Posting-Host: pm1-orl25.iag.net
- X-Newsreader: WinVN 0.99.7
-
- >gwong@cse.unsw.edu.au (Geoffrey Wong) wrote:
- <snip>
- >> SLiteral *background;
- >>
- >> Now SLiteral is:-
- >>
- >> typedef struct _slit_rec *SLiteral, /* Used in rlgg */
- >>
- >> and _slit_rec is:-
- >>
- >> struct _slit_rec
- >>{
- >> char Sign; /* 0=negated, 1=pos, 2=determinate */
- >> Relation Rel;
- >> Const *Constants; /* During an rlgg process Constant and
- Var is
- >> coded using the same data type. This is
- >> possible since Const is larger than Var!
- */
- >> Which *ConstVar; /* 0=const, 1=Var for each Constants
- */
- >> int WeakLits; /* value up to this literal */
- >> Ordering *ArgOrders, /* recursive lits: =, <, >, # */
- >> *SaveArgOrders; /* copy during pruning */
- >> float Bits; /* encoding length */
- >>};
- >>Now my question is can I just say background[i]->Sign = ???? or do I need to
- >>allocate space for background[i] or do I need to allocate space for
- *background
- >>Any help would be appreciated, I am rather new to c, don't know much tricks
- >>yet.
-
- When you are working with pointers, you must initialize the pointer to a
- valid object before you attempt to dereference it. So, yes, you need to
- either allocate space or set the pointer to the address of an existing object.
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- struct _slit_rec /* elements removed for simplicity */
- {
- char Sign; /* 0=negated, 1=pos, 2=determinate */
- int WeakLits; /* value up to this literal */
- float Bits; /* encoding length */
- };
-
- typedef struct _slit_rec *SLiteral; /* Used in rlgg */
-
- void OneDimensionalDynamic( void)
- {
- SLiteral background;
-
- /* allocate an array of struct _slit_rec */
- background = malloc( 3 * sizeof(*background));
- if( background == NULL)
- {
- fprintf( stderr, "Allocation failure.\n");
- exit( EXIT_FAILURE);
- }
-
- printf( "One dimensional dynamic version (array of objects):\n");
- background[1].Sign = '-';
- printf( "background[1].Sign == %c\n\n", background[1].Sign);
-
- /* release the memory, when no longer needed */
- free( background);
- }
-
- void TwoDimensionalDynamic( void)
- {
- SLiteral *background;
- int i;
-
- /* allocate an array of SLiteral pointers */
- background = malloc( 3 * sizeof(*background));
- if( background == NULL)
- {
- fprintf( stderr, "Allocation failure.\n");
- exit( EXIT_FAILURE);
- }
-
- /* for each SLiteral pointer, allocate at least one struct _slit_rec */
- for( i = 0; i < 3; i++)
- {
- background[i] = malloc( sizeof(*background[i]));
- if( background[i] == NULL)
- {
- fprintf( stderr, "Allocation failure.\n");
- exit( EXIT_FAILURE);
- }
- }
-
- printf( "Two dimensional dynamic version (array of pointers):\n");
- background[1]->Sign = '-';
- printf( "background[1]->Sign == %c\n", background[1]->Sign);
-
- /* if you prefer or arrays are 2 dimensional, you can use */
- background[1][0].Sign = '+';
- printf( "background[1][0].Sign == %c\n", background[1][0].Sign);
-
- /* free all memory, when no longer needed */
- for( i = 0; i < 3; i++)
- free( background[i]);
- free( background);
- }
-
- int main(void)
- {
- struct _slit_rec rec;
- SLiteral recPtr = &rec;
- SLiteral *background = &recPtr;
-
- background[0]->Sign = '-';
- printf( "Assign to the address of an existing object\n");
- printf( "background[0]->Sign[0] == %c\n\n", background[0]->Sign);
-
- OneDimensionalDynamic();
- TwoDimensionalDynamic();
-
- return 0;
- }
-
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-